package it.gestoreContratti.gui.panels;

import it.gestoreContratti.database.DatabaseController;
import it.gestoreContratti.database.exception.DatabaseConnectionException;
import it.gestoreContratti.database.exception.QueryException;
import it.gestoreContratti.entity.Contratto;
import it.gestoreContratti.entity.Periodo;
import it.gestoreContratti.exception.EmptyFieldException;
import it.gestoreContratti.inputVerifiers.NumericInputVerifier;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ProrogaContrattoPanel extends JPanel {
	
	
	private static final long serialVersionUID = -4855884078556807802L;
	private JTextField dataInizioTextField;
	private JTextField periodoTextField;
	private JTextField prorogaTextField;
	private JTextField dataScadenzaTextField;
	private DateFormat dateFormat;
	private Contratto contratto;
	private DatabaseController databaseController;

	/**
	 * Create the panel.
	 */
	public ProrogaContrattoPanel(DatabaseController dbController, Contratto c) {
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JPanel panel = (JPanel) e.getComponent();				
				panel.grabFocus();
			}
		});
		dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		this.databaseController = dbController;
		this.contratto = c;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{148, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel dataInizioLabel = new JLabel("Data Inizio");
		GridBagConstraints gbc_dataInizioLabel = new GridBagConstraints();
		gbc_dataInizioLabel.insets = new Insets(0, 0, 5, 5);
		gbc_dataInizioLabel.anchor = GridBagConstraints.EAST;
		gbc_dataInizioLabel.gridx = 0;
		gbc_dataInizioLabel.gridy = 0;
		add(dataInizioLabel, gbc_dataInizioLabel);
		
		dataInizioTextField = new JTextField();
		dataInizioTextField.setEditable(false);
		dataInizioTextField.setText(dateFormat.format(contratto.getDataInizio()));
		GridBagConstraints gbc_dataInizioTextField = new GridBagConstraints();
		gbc_dataInizioTextField.insets = new Insets(0, 0, 5, 0);
		gbc_dataInizioTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dataInizioTextField.gridx = 1;
		gbc_dataInizioTextField.gridy = 0;
		add(dataInizioTextField, gbc_dataInizioTextField);
		dataInizioTextField.setColumns(10);
		
		JLabel periodoLabel = new JLabel("Periodo");
		GridBagConstraints gbc_periodoLabel = new GridBagConstraints();
		gbc_periodoLabel.anchor = GridBagConstraints.EAST;
		gbc_periodoLabel.insets = new Insets(0, 0, 5, 5);
		gbc_periodoLabel.gridx = 0;
		gbc_periodoLabel.gridy = 1;
		add(periodoLabel, gbc_periodoLabel);
		
		periodoTextField = new JTextField();
		periodoTextField.setEditable(false);
		periodoTextField.setText(contratto.getPeriodo().toString());
		GridBagConstraints gbc_periodoTextField = new GridBagConstraints();
		gbc_periodoTextField.insets = new Insets(0, 0, 5, 0);
		gbc_periodoTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_periodoTextField.gridx = 1;
		gbc_periodoTextField.gridy = 1;
		add(periodoTextField, gbc_periodoTextField);
		periodoTextField.setColumns(10);
		
		JLabel prorogaLabel = new JLabel("Proroga");
		GridBagConstraints gbc_prorogaLabel = new GridBagConstraints();
		gbc_prorogaLabel.anchor = GridBagConstraints.EAST;
		gbc_prorogaLabel.insets = new Insets(0, 0, 5, 5);
		gbc_prorogaLabel.gridx = 0;
		gbc_prorogaLabel.gridy = 2;
		add(prorogaLabel, gbc_prorogaLabel);
		
		prorogaTextField = new JTextField();		
		prorogaTextField.setInputVerifier(new NumericInputVerifier("Proroga", 4));
		prorogaTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				if(prorogaTextField.getText().length() != 0){										
					try {						
						BigDecimal proproga = new BigDecimal(prorogaTextField.getText());
						Calendar calendar = new GregorianCalendar();
						calendar.setTime(contratto.getDataScadenza());
						if(contratto.getPeriodo().compareTo(Periodo.GG) == 0){
							calendar.add(Calendar.DAY_OF_MONTH, proproga.intValue());										
							dataScadenzaTextField.setText(dateFormat.format(calendar.getTime()));
							dataScadenzaTextField.validate();
						}
						else{
							calendar.add(Calendar.MONTH, proproga.intValue());									
							dataScadenzaTextField.setText(dateFormat.format(calendar.getTime()));
							dataScadenzaTextField.validate();			
						}						
					} catch (NumberFormatException e) {						
					}
				}
			}
		});
		GridBagConstraints gbc_prorogaTextField = new GridBagConstraints();
		gbc_prorogaTextField.insets = new Insets(0, 0, 5, 0);
		gbc_prorogaTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_prorogaTextField.gridx = 1;
		gbc_prorogaTextField.gridy = 2;
		add(prorogaTextField, gbc_prorogaTextField);
		prorogaTextField.setColumns(10);
		
		JLabel dataScadenzaLabel = new JLabel("Data Scadenza");
		GridBagConstraints gbc_dataScadenzaLabel = new GridBagConstraints();
		gbc_dataScadenzaLabel.anchor = GridBagConstraints.EAST;
		gbc_dataScadenzaLabel.insets = new Insets(0, 0, 5, 5);
		gbc_dataScadenzaLabel.gridx = 0;
		gbc_dataScadenzaLabel.gridy = 3;
		add(dataScadenzaLabel, gbc_dataScadenzaLabel);
		
		dataScadenzaTextField = new JTextField();
		dataScadenzaTextField.setText(dateFormat.format(contratto.getDataScadenza()));
		dataScadenzaTextField.setEditable(false);
		GridBagConstraints gbc_dataScadenzaTextField = new GridBagConstraints();
		gbc_dataScadenzaTextField.insets = new Insets(0, 0, 5, 0);
		gbc_dataScadenzaTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_dataScadenzaTextField.gridx = 1;
		gbc_dataScadenzaTextField.gridy = 3;
		add(dataScadenzaTextField, gbc_dataScadenzaTextField);
		dataScadenzaTextField.setColumns(10);
		
		JButton prorogaButton = new JButton("Proroga Contratto");
		prorogaButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent er) {
				int conferma = JOptionPane.showConfirmDialog(null, "Prorogare il contratto ", "", JOptionPane.YES_NO_OPTION);
				if(conferma == JOptionPane.OK_OPTION){					
					try {
						if(prorogaTextField.getText().length() == 0){
							throw new EmptyFieldException("Proroga");
						}
						BigDecimal proproga = new BigDecimal(prorogaTextField.getText());
						BigDecimal nuovaDurata = contratto.getDurata().add(proproga);
						Date dataScadenza = dateFormat.parse(dataScadenzaTextField.getText());
						databaseController.prorogaContratto(contratto.getNumero(), nuovaDurata, dataScadenza);
						JOptionPane.showMessageDialog(null, "Contratto Prorogato Correttamente", "", JOptionPane.INFORMATION_MESSAGE);
					} catch (DatabaseConnectionException e) {
						JOptionPane.showMessageDialog(null,e.getMessage() , "", JOptionPane.WARNING_MESSAGE);
					} catch (QueryException e) {
						JOptionPane.showMessageDialog(null,e.getMessage() , "", JOptionPane.WARNING_MESSAGE);
					} catch (NumberFormatException e) {						
					} catch (ParseException e) {
					} catch (EmptyFieldException e) {
						JOptionPane.showMessageDialog(null,e.getMessage() , "", JOptionPane.WARNING_MESSAGE);
					}
				}
			}
		});
		GridBagConstraints gbc_prorogaButton = new GridBagConstraints();
		gbc_prorogaButton.anchor = GridBagConstraints.SOUTHEAST;
		gbc_prorogaButton.gridx = 1;
		gbc_prorogaButton.gridy = 4;
		add(prorogaButton, gbc_prorogaButton);

	}

}
